﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;

public partial class SeachBook : System.Web.UI.Page
{
    public ArrayList aryBorrowed = new ArrayList();//列表用来存放预借信息
    private string strChooseID;//存储选择图书的ID

    /// <summary>
    /// 页面载入前
    /// </summary>
    public void Page_PreInit()
    {
        if (Session["myTheme"] != null)//判断会话中主题是否为空
        {
            this.Theme = Session["myTheme"].ToString();//设置页面主题
        }
    }

    /// <summary>
    /// 页面载入
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["Borrowed"] != null)//判断会话中预借信息是否为空
        {
            aryBorrowed = (ArrayList)(Session["Borrowed"]);//从会话中取出预借信息并填充列表
        }

        if (!IsPostBack)//如果页面不是PostBack
        {
            //绑定图书类型下拉列表框
            ddlBookType.DataTextField = BLL.CallProc.GetAllBookClass().Tables[0].Columns["ClassName"].ToString();
            ddlBookType.DataValueField = BLL.CallProc.GetAllBookClass().Tables[0].Columns["ClassID"].ToString();
            ddlBookType.DataSource = BLL.CallProc.GetAllBookClass().Tables[0];
            ddlBookType.DataBind();
        }
    }
    protected void btnConfirm1_Click(object sender, EventArgs e)
    {
    }

    /// <summary>
    /// 查询按钮点击
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        if (ddlLibrary.SelectedValue.ToString() != "OtherSchool")//如果查询本校图书
        {
            DataTable ds = BLL.CallProc.GetBookInfoByCondition(txtISBN.Text, txtBookName.Text, txtAuthor.Text, ddlBookType.SelectedValue).Tables[0];//查询并填充数据表
            GridView1.Columns.Clear();//清空列表当前信息

            //从查询结果中取出信息并生成列
            ButtonField s = new ButtonField();
            s.ButtonType = ButtonType.Link;
            s.DataTextField = ds.Columns["BookID"].ToString();
            s.CommandName = "Select";
            s.HeaderText = "图书编号";
            GridView1.Columns.Add(s);

            //从查询结果中取出信息并生成列
            BoundField bFild = new BoundField();
            bFild.DataField = ds.Columns["ISBN"].ToString();
            bFild.HeaderText = "ISBN";
            GridView1.Columns.Add(bFild);

            //从查询结果中取出信息并生成列
            bFild = new BoundField();
            bFild.DataField = ds.Columns["BookName"].ToString();
            bFild.HeaderText = "书名";
            GridView1.Columns.Add(bFild);

            //从查询结果中取出信息并生成列
            bFild = new BoundField();
            bFild.DataField = ds.Columns["Author"].ToString();
            bFild.HeaderText = "作者";
            GridView1.Columns.Add(bFild);

            //从查询结果中取出信息并生成列
            bFild = new BoundField();
            bFild.DataField = ds.Columns["PublishDate"].ToString();
            bFild.HeaderText = "出版日期";
            GridView1.Columns.Add(bFild);

            //从查询结果中取出信息并生成列
            bFild = new BoundField();
            bFild.DataField = ds.Columns["ClassName"].ToString();
            bFild.HeaderText = "所属类别";
            GridView1.Columns.Add(bFild);

            //从查询结果中取出信息并生成列
            ButtonField borrow = new ButtonField();
            borrow.HeaderText = "借阅本书";
            borrow.ButtonType = ButtonType.Button;
            borrow.Text = "借阅";
            borrow.CommandName = "Borrow";
            GridView1.Columns.Add(borrow);

            GridView1.DataSource = ds;//设置列表数据源
            GridView1.DataBind();//绑定列表
            string[] key ={ "BookID" };
            GridView1.DataKeyNames = key;//设置主键
        }
        else//如果查询外校图书
        {
            myWebService myWS = new myWebService();//生成Web Service对象
            DataTable ds = myWS.GetBookInfoByCondition(txtISBN.Text, txtBookName.Text, txtAuthor.Text, ddlBookType.SelectedValue).Tables[0];//查询并填充数据表

            GridView1.Columns.Clear();//清空列表当前信息

            //从查询结果中取出信息并生成列
            ButtonField s = new ButtonField();
            s.ButtonType = ButtonType.Link;
            s.DataTextField = ds.Columns["BookID"].ToString();
            s.CommandName = "Select";
            s.HeaderText = "图书编号";
            GridView1.Columns.Add(s);

            //从查询结果中取出信息并生成列
            BoundField bFild = new BoundField();
            bFild.DataField = ds.Columns["ISBN"].ToString();
            bFild.HeaderText = "ISBN";
            GridView1.Columns.Add(bFild);

            //从查询结果中取出信息并生成列
            bFild = new BoundField();
            bFild.DataField = ds.Columns["BookName"].ToString();
            bFild.HeaderText = "书名";
            GridView1.Columns.Add(bFild);

            //从查询结果中取出信息并生成列
            bFild = new BoundField();
            bFild.DataField = ds.Columns["Author"].ToString();
            bFild.HeaderText = "作者";
            GridView1.Columns.Add(bFild);

            //从查询结果中取出信息并生成列
            bFild = new BoundField();
            bFild.DataField = ds.Columns["PublishDate"].ToString();
            bFild.HeaderText = "出版日期";
            GridView1.Columns.Add(bFild);

            //从查询结果中取出信息并生成列
            bFild = new BoundField();
            bFild.DataField = ds.Columns["ClassName"].ToString();
            bFild.HeaderText = "所属类别";
            GridView1.Columns.Add(bFild);

            GridView1.DataSource = ds;//设置列表数据源
            GridView1.DataBind();//绑定列表
            string[] key ={ "BookID" };
            GridView1.DataKeyNames = key;//设置主键

        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Borrow")
        {
            int index = Convert.ToInt16(e.CommandArgument);
            strChooseID=(GridView1.DataKeys[index].Value.ToString());
            if (!BLL.CallProc.IsBorrowed(strChooseID))
            {
                this.Response.Write("<script>alert('"  + GridView1.Rows[index].Cells[2].Text +"已借出"+ "');</script>");
                return;
            }

            if (!aryBorrowed.Contains(strChooseID))
            {
                int i = aryBorrowed.Add(strChooseID);
                this.Response.Write("<script>alert('" + "借阅图书：" +GridView1.Rows[index].Cells[2].Text+ "');</script>");
                Session["Borrowed"] = aryBorrowed;
            }
            else
            {
                this.Response.Write("<script>alert('" + "图书：" +GridView1.Rows[index].Cells[2].Text+"已经借阅"+ "');</script>");
            }
        }
    }
}
